package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDTO;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.scripts.JO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags = "03.评论模块")
@RestController
@RequestMapping("/v1/comment")
public class CommentController {


    @Autowired
    private CommentMapper commentMapper;
    /**
     * 1.发表评论功能
     */
    @ApiOperation(value = "发表评论功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO , @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null){//未登录
//        return new JsonResult(StatusCode.NO_LOGIN);
//    }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);

        return  JsonResult.ok();}
    /**
     * 2.查看评论列表功能
     * @param id 微博id
     * @return List集合
     */
    @ApiOperation(value = "获取评论列表")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "id", value = "微博编号", required = true, dataType = "int")
    public JsonResult selectByWeiboId(int id){
        List<CommentVO> commentVOS=commentMapper.selectByWeiboId(id);
        return  JsonResult.ok(commentVOS);

    }}
